using UnityEngine;
using System.IO;
using System.Collections;

public class LIBFile
{
	public delegate void ReaderErrorDelegate();
	public delegate void ReaderDelegate(BinaryReader br);
	public delegate void WriteDelegate(BinaryWriter bw);

	public static string GetLocalPersistentDataPath()
	{
		string path = Application.persistentDataPath + "/Documents/";
		
		try
		{
			if (!Directory.Exists(path))
				Directory.CreateDirectory(path);
			
		}
		catch (System.Exception ex) { }

		return path;
	}

    public static bool IsPersistentFileExit(string name)
    {
        return IsLocalFileExit(GetLocalPersistentDataPath() + name);
    }

	/// <summary>
	/// 检测文件是否存在
	/// </summary>
	/// <param name="fileName"></param>
	/// <returns></returns>
	public static bool IsLocalFileExit(string path)
	{
		return File.Exists(path);
	}

	/// <summary>
	/// 删除本地文件
	/// </summary>
	/// <param name="path">Path.</param>
	public static void DeleteLocalFile(string path)
	{
		File.Delete(path);
	}

    /// <summary>
    /// 删除本地文件
    /// </summary>
    /// <param name="path">Path.</param>
    public static void DeletePersistentFile(string name)
    {
        File.Delete(GetLocalPersistentDataPath() + name);
    }

    /// <summary>
    /// 读取持久数据文件
    /// </summary>
    /// <param name="fileName"></param>
    /// <param name="onRead"></param>
    public static byte[] LoadPersistentFile(string fileName)
    {
        return LoadLocalFile(GetLocalPersistentDataPath() + fileName);
    }

	/// <summary>
	/// 读取本地文件
	/// </summary>
	/// <param name="fileName"></param>
	/// <returns></returns>
	public static byte[] LoadLocalFile(string path)
	{
		if (IsLocalFileExit(path))
		{
			Stream stream = File.Open(path, FileMode.Open);
			byte[] buffer = new byte[(int)stream.Length];
			stream.Read(buffer, 0, (int)stream.Length);
			stream.Close();
			
			return buffer;
		}
		else
		{
			return null;
		}
		
	}
	
	/// <summary>
	/// 读取本地文件
	/// </summary>
	/// <param name="fileName"></param>
	/// <param name="onRead"></param>
	public static bool LoadLocalFile(string path, ReaderDelegate onRead)
	{
		bool result = false;
		byte[] buffer = LoadLocalFile(path);
		if (buffer != null)
		{
			MemoryStream ms = null;
			BinaryReader br = null;
			try
			{
				ms = new MemoryStream(buffer);
				br = new BinaryReader(ms);
				
				if (onRead != null) onRead(br);

				result = true;
			}
			catch (System.Exception e)
			{
                Debug.LogError(e.Message);
			}
			finally
			{
				if (br != null)
				{
					br.Close();
					br = null;
				}
				if (ms != null)
				{
					ms.Close();
					ms = null;
				}
			}
		}

		return result;
	}
	
	/// <summary>
	/// 读取持久数据文件
	/// </summary>
	/// <param name="fileName"></param>
	/// <param name="onRead"></param>
	public static bool LoadPersistentFile(string fileName, ReaderDelegate onRead)
	{
		return LoadLocalFile(GetLocalPersistentDataPath() + fileName, onRead);
	}
	
	/// <summary>
	/// 存储本地文件
	/// </summary>
	/// <param name="fileName"></param>
	/// <param name="content"></param>
	public static void SaveLocalFile(string path, string fileName, byte[] content)
	{
		if (!Directory.Exists(path))
		{
			Directory.CreateDirectory(path);
		}
		
		Stream stream = null;
		stream = File.Open(path + fileName, FileMode.Create);
		stream.Write(content, 0, content.Length);
		stream.Flush();
		stream.Close();
	}
	
	/// <summary>
	/// 存储本地文件
	/// </summary>
	/// <param name="fileName"></param>
	public static void SaveLocalFile(string path, string fileName, WriteDelegate onWrite)
	{
		MemoryStream ms = new MemoryStream();
		BinaryWriter bw = new BinaryWriter(ms);
		
		byte[] buffer = null;
		
		try
		{
			if (onWrite != null) onWrite(bw);
			buffer = ms.ToArray();
			SaveLocalFile(path, fileName, buffer);
		}
		catch (System.Exception e)
		{
			Debug.LogError(e.Message);
		}
		finally
		{
			bw.Close();
			ms.Close();
			bw = null;
			ms = null;
		}
	}
	
	/// <summary>
	/// 存储持久数据文件
	/// </summary>
	/// <param name="fileName"></param>
	public static void SavePersistentFile(string fileName, byte[] content)
	{
		SaveLocalFile(GetLocalPersistentDataPath(), fileName, content);
	}
	
	/// <summary>
	/// 存储持久数据文件
	/// </summary>
	/// <param name="fileName"></param>
	public static void SavePersistentFile(string fileName, WriteDelegate onWrite)
	{
		SaveLocalFile(GetLocalPersistentDataPath(), fileName, onWrite);
	}
}


